package com.komodo.island.gdx.screen;

import com.badlogic.gdx.graphics.Texture;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.Input;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.dialog.Dialog;
import com.komodo.island.gdx.dialog.LoseDialog;
import com.komodo.island.gdx.dialog.StarterDialog;
import com.komodo.island.gdx.dialog.WinDialog;
import com.komodo.island.gdx.game.kodomosuit.SuitKomodo;
import com.komodo.island.gdx.game.komodobalance.GameBalance;
import com.komodo.island.gdx.game.komodomove.KomodoMove;
import com.komodo.island.gdx.game.komodophoto.KomodoPhoto;
import com.komodo.island.gdx.game.sandcastle.SandCastle;
import com.komodo.island.gdx.game.underwater.KomodoUnderwater;

public abstract class GameScreen extends Screen {

	public static final int GAME_PAUSE = 3;
	public static final int GAME_WIN = 2;
	public static final int GAME_LOSE = 1;
	public static final int GAME_RUNNING = 0;
	public static final int GAME_START_ANIMATION = 4;

	public int gameState;
	public int score;
	public int bestScore;
	protected WinDialog winDialog;
	protected LoseDialog loseDialog;

	protected Dialog startDialog;

	protected double startTime;
	protected double currTime;

	public GameScreen() {
		// TODO Auto-generated constructor stub
		ID = Input.SCREEN_GAME;
	}

	/*
	 * override init method and code one line to add previous screen for this
	 * screen
	 */

	@Override
	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);
		// this.winDialog = new Suit(Constants.GAME_WIDTH,
		// Constants.GAME_HEIGHT, true, this);
		// this.loseDialog = new LoseDialog(Constants.GAME_WIDTH,
		// Constants.GAME_HEIGHT, true, this);

		gameState = GAME_START_ANIMATION;
	}

	@Override
	public void onScreenRun() {
		// TODO Auto-generated method stub
		// System.out.println("screen run");
		camera.update();
		batch.setProjectionMatrix(camera.combined);
		if (gameState == GAME_START_ANIMATION) {
			if (!onGameStartAnimation())
				gameState = GAME_RUNNING;
		} else if (gameState == GAME_RUNNING) {
			onGameRunning();
			// System.out.println("game running");
		}
	}

	@Override
	public void onScreenLoading() {
		// TODO Auto-generated method stub
		onGameLoading();
		if (startDialog == null) {

			startDialog = komodoIsland.startDialog;
			((StarterDialog) startDialog).setScreen(this);
		} else {

		}
		// setScreenToRun();
		startTime = System.currentTimeMillis();
		onGameStart();

	}

	@Override
	public void onScreenPause() {
		// TODO Auto-generated method stub
		// System.out.println("pause");
		if (gameState == GAME_WIN) {
			winDialog.draw();
			onGameWin();
		} else if (gameState == GAME_LOSE) {
			loseDialog.draw();
			onGameLose();
		} else if (gameState == GAME_PAUSE) {
			pauseDialog.draw();
			onGamePause();
		}

	}

	/*
	 * To derive new class, you must override these methods
	 */

	protected abstract void onGameRunning();

	protected abstract void onGameWin();

	protected abstract void onGameLose();

	protected abstract void onGamePause();

	protected abstract void onGameStart();

	protected abstract void onGameLoading();

	protected boolean onGameStartAnimation() {
		currTime = System.currentTimeMillis();
		startDialog.draw();

		Art.drawString(startDialog.getSpriteBatch(), "" + bestScore, 178, 0);
		if (currTime - startTime <= 1000) {
			// Art.drawString("3", 240 - 32, 160 - 32);
			startDialog.getSpriteBatch().begin();
			startDialog.getSpriteBatch().draw(komodoIsland.startupAnimation[0],
					240 - 270 / 2, 160 - 270 / 2);
			startDialog.getSpriteBatch().end();

			return true;
		} else if (currTime - startTime > 1000 && currTime - startTime <= 2000) {
			// Art.drawString("2", 240 - 32, 160 - 32);
			startDialog.getSpriteBatch().begin();
			startDialog.getSpriteBatch().draw(komodoIsland.startupAnimation[1],
					240 - 270 / 2, 160 - 270 / 2);
			startDialog.getSpriteBatch().end();

			return true;
		} else if (currTime - startTime > 2000 && currTime - startTime <= 3000) {
			// Art.drawString("1", 240 - 32, 160 - 32);
			startDialog.getSpriteBatch().begin();
			startDialog.getSpriteBatch().draw(komodoIsland.startupAnimation[2],
					240 - 270 / 2, 160 - 270 / 2);
			startDialog.getSpriteBatch().end();

			return true;
		} else {
			return false;
		}
	}

	/*
	 * You can call these methods to change the game state
	 */

	protected void setGameToRun() {
		setScreenToRun();
		gameState = GAME_RUNNING;
	}

	protected void setGameToPause() {
		setScreenToPause();
		gameState = GAME_PAUSE;
	}

	protected void setGameToWin() {
		setCameraToDefault();
		// System.out.println(score);
		try {
			((WinDialog) winDialog).hideAllNotif();
		} catch (Exception e) {
			// TODO: handle exception
		}

		boolean levelUnlocked;
		int id = 0;
		if (score > bestScore) {

			if (this instanceof SuitKomodo) {
				id = Persistence.SUIT;
			} else if (this instanceof KomodoMove) {
				id = Persistence.MOVE;
			} else if (this instanceof KomodoPhoto) {
				id = Persistence.PHOTO;
			} else if (this instanceof KomodoUnderwater) {
				id = Persistence.UNDERWATER;
			} else if (this instanceof GameBalance) {
				id = Persistence.BALANCE;
//			} 
//			else if (this instanceof ShoppingGame) {
//				id = Persistence.SHOPPING;
			} else if (this instanceof SandCastle) {
				id = Persistence.CASTLE;
			}
			if (id < 5) {
				levelUnlocked = Persistence.unlockLevel(id + 1);
				if (levelUnlocked) {
					((WinDialog) winDialog).showUnlockLevel();
				}
			}

			((WinDialog) winDialog).showHighscore();

			Persistence.newBestScore(id, score);
		}
		if (Persistence.checkLevelPiece(id, score, true)) {
			((WinDialog) winDialog).showNewPiece();
		}

		setScreenToPause();
		komodoIsland.input.setStage((Dialog) winDialog);
		gameState = GAME_WIN;
	}

	protected void setGameToLose() {

		setScreenToPause();
		try {
			((LoseDialog) loseDialog).hideAllNotif();
		} catch (Exception e) {
			// TODO: handle exception
		}

		int id = 0;
		if (this instanceof SuitKomodo) {
			id = Persistence.SUIT;
		} else if (this instanceof KomodoMove) {
			id = Persistence.MOVE;
		} else if (this instanceof KomodoPhoto) {
			id = Persistence.PHOTO;
		} else if (this instanceof KomodoUnderwater) {
			id = Persistence.UNDERWATER;
		} else if (this instanceof GameBalance) {
			id = Persistence.BALANCE;
//		}
//		else if (this instanceof ShoppingGame) {
//			id = Persistence.SHOPPING;
		} else if (this instanceof SandCastle) {
			id = Persistence.CASTLE;
		}
		if (Persistence.checkLevelPiece(id, score, false)) {
			((LoseDialog) loseDialog).showNewPiece();
		}
		komodoIsland.input.setStage((Dialog) loseDialog);
		gameState = GAME_LOSE;
	}

	/*
	 * use this method to exit the screen, back to the map selection
	 */

	protected void endGame() {
		exitScreen();
	}

	@Override
	public void exitScreen() {
		// TODO Auto-generated method stub
//		cleanResScreen();
		super.exitScreen();
		
	}
	
	public void startGame() {
		setCameraToDefault();

		// startTime = System.currentTimeMillis();

		int id = 0;
		if (this instanceof SuitKomodo) {
			id = Persistence.SUIT;
		} else if (this instanceof KomodoMove) {
			id = Persistence.MOVE;
		} else if (this instanceof KomodoPhoto) {
			id = Persistence.PHOTO;
		} else if (this instanceof KomodoUnderwater) {
			id = Persistence.UNDERWATER;
		} else if (this instanceof GameBalance) {
			id = Persistence.BALANCE;
//		}
//		else if (this instanceof ShoppingGame) {
//			id = Persistence.SHOPPING;
		} else if (this instanceof SandCastle) {
			id = Persistence.CASTLE;
		}
		// System.out.println("ID" + id);
		bestScore = Persistence.getBestScore(id);
		// ((WinDialog)winDialog).hideAllNotif();
		screenState = LOADING;
		gameState = GAME_START_ANIMATION;
		onScreenLoading();

	}

	public void retryGame() {
		// setScreenToRun();
		startGame();
	}

	public void resumeGame() {
		if (gameState == GAME_PAUSE)
			setGameToRun();
	}

	public void pauseGame() {
		if (gameState == GAME_RUNNING)
			setGameToPause();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		super.dispose();
		

	}

	@Override
	public void cleanResScreen() {
		// TODO Auto-generated method stub
		super.cleanResScreen();
		try {
			winDialog.dispose();
			loseDialog.dispose();
//			winDialog= null;
//			loseDialog =null;
			// startDialog.dispose();
		} catch (Exception e) {

		}
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		// startTime = System.currentTimeMillis();
		if (Persistence.sound) {
			if (komodoIsland.main.isPlaying()) {
				komodoIsland.main.stop();
			} else {

			}
			if (komodoIsland.scientist.isPlaying()) {
				komodoIsland.scientist.stop();
			} else {

			}
			if (komodoIsland.tourist.isPlaying()) {
				komodoIsland.tourist.stop();
			} else {

			}

		} else {
			komodoIsland.main.stop();
			if (komodoIsland.scientist.isPlaying())
				komodoIsland.scientist.stop();

		}
	}
	
	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
//		try {
//			try {
//				winDialog.dispose();
//				loseDialog.dispose();
//			} catch (Exception e) {
//
//			}
//			winDialog = null;
//			loseDialog = null;
//			System.gc();
//			
//		} catch (Exception e) {
//
//		}
	}

}
